package com.mybatisplus.controller;

import com.mybatisplus.entity.BaseResultBean;
import com.mybatisplus.service.StudentService;
import com.mybatisplus.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


//页面控制

/**
 * @author yancheng
 * @date 2023/3/31
 * apiNote
 */
@RestController
//@CrossOrigin(origins = "*" ,maxAge = 3600)
@RequestMapping("/student")
public class StudentController {
    //1.Get是查询请求，用来获取资源
    //2.Post是用来新建资源的，也可以用来更新
    //3.Put用来更新
    //4.Delete用来删除
    @Autowired
    private StudentService studentService;


    //    根据选中的id进行删除
    @PostMapping("/del")
    public BaseResultBean deleteUserByIds(int ids) {
        studentService.deleteUserByIds(ids);
        BaseResultBean baseResultBean = new BaseResultBean();
        baseResultBean.setMessage("成功");
        baseResultBean.setStatus(true);
        return baseResultBean;
    }

    //    对学员分数的修改
    @PutMapping("/update")
    public BaseResultBean updateScoreById(int id, int score) {
        studentService.updateScoreById(id, score);
        BaseResultBean baseResultBean = new BaseResultBean();
        baseResultBean.setMessage("修改成功");
        baseResultBean.setStatus(true);
        return baseResultBean;
    }

    @RequestMapping("/alterScore")
    public BaseResultBean alterScore(HttpServletRequest request) {
        BaseResultBean bean = new BaseResultBean();
        try {
            Integer option = Integer.valueOf(request.getParameter("score"));
            Integer score = studentService.selectStudentByStuid(request.getParameter("stuid")).getScore();
            studentService.updateScoreByStuid(option + score, request.getParameter("stuid"));
            bean.setMessage("分数修改成功");
            bean.setStatus(true);
        } catch (Exception e) {
            bean.setMessage(e.getMessage());
            bean.setStatus(false);
        }
        return bean;
    }

    //    随机选择三位学生
    @GetMapping("/choose")
    public StudentVO chooseStudent(HttpServletRequest request, HttpServletResponse response) {
        StudentVO studentVO = new StudentVO();
        if (request.getSession(false) != null) {
            String random = request.getParameter("random");
            studentVO = studentService.choose(Integer.parseInt(random));
        } else {
            try {
                response.sendError(302, "-1");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return studentVO;
    }

    //    接收名字进行查询(模糊查询)
    @GetMapping("/name")
    public StudentVO selName(String name) {
        return studentService.selectName(name);
    }

    //通过学号获取学生姓名
    @GetMapping("/getStudentName")
    public String getStudentName(HttpServletRequest request) {
        String stuid = request.getParameter("stuid");
        return studentService.selectNameByStuid(stuid);
    }

    //通过学号获取单个学生信息
    @RequestMapping("getStudent")
    public StudentVO getStudent(HttpServletRequest request) {
        StudentVO studentVO = new StudentVO();
        System.out.println(request.getParameter("stuid"));
        studentVO.setData(studentService.selectStudentByStuid(request.getParameter("stuid")));
        studentVO.setCode(1);
        studentVO.setCount(1);
        return studentVO;
    }

    //修改密码请求
    @RequestMapping("alterPwd")
    public BaseResultBean alterPwd(HttpServletRequest request) {
        String stuid = request.getParameter("stuid");
        String newPassword = request.getParameter("newPassword");
        return studentService.alterPassword(stuid, newPassword, request);
    }


}

